package wikiArticleTypeMatch;

import zclTool.*;
import java.util.*;
import java.io.*;

public class AttributeType {

	/**
	 * @param args
	 */
	static int sentencePos=4;
	static int rphmatchpos=4;
	static int rphattriid=2;
	static int mymatchpos = 5;
	static int mymatchvalues = 6;
	static DelimitedReader drRph;
	//static DelimitedReader drMy;
	//static String []mybuf ;
	static String []rphbuf;
	
	public static void main(String[] args) throws Exception{
		// TODO Auto-generated method stub
		DelimitedReader drmy = new DelimitedReader(args[0]);
		DelimitedReader drrph = new DelimitedReader(args[1]);
		DelimitedReader drsen = new DelimitedReader(args[2]);
		OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(args[3]),"utf-8");
		int id = 0;

		String []buffermy = new String[100];
		String []bufferrph = new String[100];
		String []bufferSen = new String[100];
		int numjoin =0;
		do{
			ArrayList<String []>listmy = new ArrayList<String []>();
			ArrayList<String []>listrph = new ArrayList<String []>();
			ArrayList<String []>listsen = new ArrayList<String []>();
			int idmy = dealNext(id,listmy,drmy,buffermy,2);
			int idrph = dealNext(id,listrph,drrph,bufferrph,0);
			dealNext(id,listsen,drsen,bufferSen,0);
			
			if(listmy.size()>0 && listrph.size()>0){
				//System.out.println(id);
				
				join(listmy,listrph,listsen,osw);
				if(numjoin++ % 10000 ==0)System.out.println(id);
			}
			if(idmy == -1 || idrph == -1){
				break;
			}
			id = Math.max(idmy, idrph);
		}while(true);
		drmy.close();
		drrph.close();
		drsen.close();
		osw.close();
	}
	
	public static int dealNext(int id, ArrayList<String[]>lists, DelimitedReader dr, String[] buffer,int keyid)throws Exception{
		String []line = null;
		if(buffer[keyid]!=null && Integer.parseInt(buffer[keyid]) > id){
			return Integer.parseInt(buffer[keyid]);
		}else if(buffer[keyid] == null || Integer.parseInt(buffer[keyid])==id){
			if(buffer[keyid] != null && Integer.parseInt(buffer[keyid])==id){
				String []temp = new String[buffer.length];
				System.arraycopy(buffer, 0, temp, 0, buffer.length);
				lists.add(temp);
			}
			while((line = dr.read())!=null){
				if(Integer.parseInt(line[keyid])==id){
					String []temp = new String[line.length];
					System.arraycopy(line, 0, temp, 0, line.length);
					lists.add(temp);
				}else{
					//buffer=line;
					System.arraycopy(line, 0, buffer, 0, line.length);
					break;
					//return Integer.parseInt(buffer[2]);
				}
			}
		}else{
			while((line = dr.read())!=null){
				int lineid = Integer.parseInt(line[keyid]);
				System.out.println(lineid);
				if(lineid==id){
					String []temp = new String[line.length];
					System.arraycopy(line, 0, temp, 0, line.length);
					lists.add(temp);
				}else if(lineid >id){
					System.arraycopy(line, 0, buffer, 0, line.length);
					break;
					//return Integer.parseInt(buffer[2]);
				}else continue;
			}
		}
		if(line == null){
			return -1;
		}else{
			return Integer.parseInt(buffer[keyid]);
		}
	}
	
	public static void join(ArrayList<String []>listmy,ArrayList<String []>listrph,ArrayList<String []>listsen, OutputStreamWriter osw){
		
		String sentence_str = listsen.get(0)[sentencePos];
		String []sentence = sentence_str.replace("\\t", "\t").split("\t");
		
		for(int i=0;i<listmy.size();i++){
			ArrayList<String>myvalues = new ArrayList<String>();
			
			String []my = listmy.get(i);
			int wpid = Integer.parseInt(my[1]);
			int senid = Integer.parseInt(my[2]);
			String type = my[3];
			String prop = my[4];
			String []mymatch = my[mymatchpos].replace("[","").replace("]", "").split(",");
			String []mymatchvalue = my[mymatchvalues].split("\\|");
			for(int k=0;k<mymatchvalue.length && k<mymatch.length;k++){
				if(!mymatch.equals("-1")){
					myvalues.add(mymatchvalue[k]);
				}
			}
			for(int j=0;j<listrph.size();j++){
				String []rph = listrph.get(j);
				if(rph.length<rphmatchpos || rph[rphmatchpos] == null) continue;
				ArrayList<String>rphvalues = new ArrayList<String>();
				int attributeid = Integer.parseInt(rph[rphattriid]);
				try{
					String rphpos_str = rph[rphmatchpos];
					String []split = rphpos_str.split(" |:");
					for(int k=0;k<split.length;k+=2){
						int a = Integer.parseInt(split[0]);
						int b = Integer.parseInt(split[1]);
						String value = "";
						for(int s=a;s<b;s++){
							value+=sentence[s]+" ";
						}
						rphvalues.add(value);
						//System.out.println(attributeid+"\t"+value);
					}
					if(isMatch(myvalues,rphvalues)){
						osw.write(wpid+"\t"+senid+"\t"+type+"\t"+prop+"\t"+attributeid+"\t"+myvalues+"\t"+rphvalues+"\n");
						System.out.println(wpid+"\t"+senid+"\t"+type+"\t"+prop+"\t"+attributeid+"\t"+myvalues+"\t"+rphvalues);
					}
				}catch(Exception e){
					
				}
			}
		}
	}

	public static boolean isMatch(ArrayList<String>my, ArrayList<String>rph){
		boolean res = false;
		for(String a:my)for(String b:rph){
			if(a.toLowerCase().equals(b.toLowerCase()))res = true;
		}
		return res;
	}
}
